package spring.aop;

import spring.transaction.Transaction;

import java.util.ArrayList;
import java.util.List;

public class Advice {

    private final List<AdviceMethod> attributes = new ArrayList<>(8);
    private String id;
    private Transaction transaction;

    public Transaction getTransaction() {
        return transaction;
    }

    public void setTransaction(Transaction transaction) {
        this.transaction = transaction;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void addMethod(AdviceMethod adviceMethod) {
        attributes.add(adviceMethod);
    }

    public AdviceMethod getAdviceMethod(String methodSignature) {
        AdviceMethod result = null;
        for (AdviceMethod adviceMethod : attributes) {
            if ("*".equals(adviceMethod.getName())) {
                result = adviceMethod;
            } else if (methodSignature.matches(adviceMethod.getRegular())) {
                result = adviceMethod;
                break;
            }
        }
        return result;
    }

}
